<template>
  <div>
    <!-- 导航栏 -->
    <van-nav-bar title="编辑资料" left-arrow @click-left="$router.go(-1)" />
    <div class="photo">
      <van-image
        width="120"
        height="120"
        round
        fit="cover"
        :src="userInfo.photo"
      />
      <van-uploader :after-read="afterRead" />
    </div>
    <van-cell-group>
      <van-cell
        title="名称"
        is-link
        :value="userInfo.name"
        @click="showDialogClick"
      />
      <van-cell
        title="性别"
        is-link
        :value="userInfo.gender ? '女' : '男'"
        @click="showPopupClick"
      />
      <van-cell
        title="生日"
        is-link
        :value="userInfo.birthday"
        @click="showBirthdayClick"
      />
    </van-cell-group>
    <!-- 修改昵称的对话框 -->
    <van-dialog
      v-model="showDialog"
      title="修改昵称"
      show-cancel-button
      @confirm="updateNickname"
    >
      <!-- //自动聚焦 ref -->
      <van-field
        ref="nameFiled"
        placeholder="请输入昵称"
        v-model="newName"
      ></van-field>
    </van-dialog>
    <!-- 性别弹窗 -->
    <van-popup v-model="showProup" position="bottom">
      <van-nav-bar
        title="更改性别"
        left-arrow
        @click-left="showProup = false"
      />
      <van-cell title="男" is-link @click="updateGender(0)"></van-cell>
      <van-cell title="女" is-link @click="updateGender(1)"></van-cell>
    </van-popup>
    <van-popup position="bottom" v-model="showBirthday">
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
        @cancel="showBirthday = false"
        @confirm="updateBirthday"
      />
    </van-popup>
    <!-- 截切区域 -->
    <div class="cropper-box" v-show="showCropper">
      <!-- 组件 -->
      <vueCropper
        ref="cropper"
        fixed
        autoCropWidth="120"
        autoCropheight="120"
        autoCrop
        :img="img"
      ></vueCropper>
      <van-button @click="submitPhoto" class="confirm-btn" type="primary"
        >确认</van-button
      >
      <van-button @click="showCropper = false" type="info" class="cancel-btn"
        >取消</van-button
      >
    </div>
  </div>
</template>

<script>
import { VueCropper } from "vue-cropper";
import moment from "moment";
import { editUserInfo, editUserAvatar } from "./user.js";
import { mapState } from "vuex";
export default {
  name: "edit",

  data() {
    return {
      showDialog: false,
      newName: "",
      showProup: false,
      minDate: new Date(1980, 0, 1),
      maxDate: new Date(2025, 10, 1),
      currentDate: new Date(),
      showBirthday: false,
      showCropper: false,
      img: "",
      fileList: [
        { url: "https://img01.yzcdn.cn/vant/leaf.jpg" },
        // Uploader 根据文件后缀来判断是否为图片文件
        // 如果图片 URL 中不包含类型信息，可以添加 isImage 标记来声明
        { url: "https://cloud-image", isImage: true },
      ],
    };
  },

  showProup() {
    this.show = true;
  },

  methods: {
    //修改姓名
    async updateNickname() {
      await editUserInfo({ name: this.newName });
      this.$toast.success("更新成功");
      this.$store.commit("setUserInfo", {
        ...this.user,
        name,
      });
    },
    //弹出输入框
    showDialogClick() {
      this.showDialog = true;
      this.newName = this.userInfo.name;
      // 高级计时器,只有vue中有
      this.$nextTick(() => {
        this.$refs.nameFiled.focus();
      });
    },
    //弹出隐藏框
    showPopupClick() {
      this.showProup = true;
    },
    showBirthdayClick() {
      this.showBirthday = true;
    },
    //更改性别
    async updateGender(gender) {
      await editUserInfo({ gender });
      //弹窗提醒更改成功
      this.$toast.success("更新成功");
      //更新vuex数据
      this.$store.commit("setUserInfo", {
        //...铺开
        ...this.userInfo,
        gender,
      });
      this.showProup = false;
    },
    //修改生日
    async updateBirthday() {
      // console.log(this.currentDate);
      let birthday = moment(this.currentDate).format("YYYY-MM-DD");
      await editUserInfo({ birthday });
      //(1)弹窗提示
      this.$toast.success("更新成功!");
      //(2)更新vuex数据
      this.$store.commit("setUserInfo", {
        //...铺开
        ...this.userInfo,
        birthday,
      });
      this.showBirthday = false;
    },
    //上传头像
    afterRead(file) {
      this.img = file.content;
      //(2)弹出裁切框
      this.showCropper = true;
    },
    //确认按钮剪切上传图片
    submitPhoto() {
      // 获取Blob这种格式
      // formData + 接口提交到远程
      this.$refs.cropper.getCropBlob(async (blob) => {
        const fd = new FormData();
        fd.append("photo", blob);
        await editUserAvatar(fd);
        //(1)弹窗提示
        this.$toast.success("更新成功!");
        //(2)更新vuex数据
        this.$store.commit("setUserInfo", {
          ...this.userInfo,
          photo: res.data.data.photo,
        });
        //(3)关闭裁切窗口
        this.showCropper = false;
      });
    },
  },
  computed: {
    ...mapState(["userInfo"]),
  },
  components: {
    VueCropper,
  },
};
</script>

<style lang="less" >
.photo {
  text-align: center;
  padding: 20px 0;
  .van-image {
    left: 20%;
  }
}

.van-uploader {
  position: absolute;
  opacity: 0;
  left: -10%;
  transform: translateY(-40%);
  // transform: translateX(-10%);
  width: 120px;
  height: 120px;
  .van-uploader__upload {
    width: 120px;
    height: 120px;
  }
}
.cropper-box {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  .confirm-btn {
    position: absolute;
    left: 0;
    bottom: 0;
  }
  .cancel-btn {
    position: absolute;
    right: 0;
    bottom: 0;
  }
}
</style>